
function init() {
    jQuery("#formID").validationEngine();
    $("#captcha-id").bind('keyup',function(event){
        if(event.keyCode == 13) {
            login();
        }
    });
    $("#login-button-id").bind('mouseover',function(event){
        $("#login-button-id").css("background-color", "#5091DF");
    });

    $("#login-button-id").bind('mouseout',function(event){
        $("#login-button-id").css("background-color", "#67AEF7");
    });

    var usernameInput = $("#username-input-id");
    var passwordInput = $("#password-input-id");
    var captchaId = $("#captcha-id");

    var usernameDiv = $("#username-div-id");
    var passwordDiv = $("#password-div-id");
    var checkimageInput = $("#checkimage-input-id");

    usernameInput.focus(function(){
        $(this).css("background","#E2F0FD");
        usernameDiv.css("background","#E2F0FD");
    });

    usernameInput.blur(function(){
        $(this).css("background","#FFFFFF");
        usernameDiv.css("background","#FFFFFF");
    });

    passwordInput.focus(function(){
        $(this).css("background","#E2F0FD");
        passwordDiv.css("background","#E2F0FD");
    });

    passwordInput.blur(function(){
        $(this).css("background","#FFFFFF");
        passwordDiv.css("background","#FFFFFF");
    });

    captchaId.focus(function(){
        $(this).css("background","#E2F0FD");
        checkimageInput.css("background","#E2F0FD");
    });

    captchaId.blur(function(){
        $(this).css("background","#FFFFFF");
        checkimageInput.css("background","#FFFFFF");
    });

    usernameInput.bind('mouseover',function(event){
        usernameDiv.css("border", "solid 1px #99BBE8");
    });

    usernameInput.bind('mouseout',function(event){
        usernameDiv.css("border", "solid 1px #D9D9D9");
    });

    passwordInput.bind('mouseover',function(event){
        passwordDiv.css("border", "solid 1px #99BBE8");
    });

    passwordInput.bind('mouseout',function(event){
        passwordDiv.css("border", "solid 1px #D9D9D9");
    });

    captchaId.bind('mouseover',function(event){
        checkimageInput.css("border", "solid 1px #99BBE8");
    });

    captchaId.bind('mouseout',function(event){
        checkimageInput.css("border", "solid 1px #D9D9D9");
    });
}

function refreshCode(obj){
    obj.src = obj.src + "?code=" + Math.random();
}


function login(){
    $("#login-button-id").css("background-color", "#2055AF");
    $("#resule-span").text("");
    var usernameElementInput = $("#username-input-id");
    var passwordElementInput = $("#password-input-id");
    var captchaElementInput = $("#captcha-id");

    var captcha = captchaElementInput.val()
    var username = usernameElementInput.val();
    var password = passwordElementInput.val();

    console.log("-----" + username + "----" + password + "----" + captcha);

    if (!username || username == "" || username.trim() == "") {
        $("#resule-span").append("用户名不可为空");
        $("#result").css("visibility", "visible");
        return;
    }

    if (!password || password == "" || password.trim() == "") {
        $("#resule-span").append("密码不可为空");
        $("#result").css("visibility", "visible");
        return;
    }

    if (!captcha || captcha == "" || captcha.trim() == ""){
        $("#resule-span").append("验证码不可为空");
        $("#result").css("visibility", "visible");
        return;
    }

    $.ajax({
        type: 'POST',
        url: "/cms/manage/login",
        data: {username:username, password:password, captcha:captcha},
        success: function success(data){
            console.log("-----", data);
        	if (data.errorCode && data.errorCode != 302) {
                $("#resule-span").append(data.message);
                $("#result").css("visibility", "visible");
                return;
			}

			console.log("跳转");
            window.location.href="/cms/manage/index";

        },
		error:function(data){
            $("#resule-span").append(data.message);
            $("#result").css("visibility", "visible");
        }
    });

}